/* eslint-disable no-useless-concat */
/* eslint-disable prefer-template */
/* eslint-disable camelcase */
import program from 'commander';

import { VERSION } from './utils/constants';

import apply from './index';

const actionMap = {
  install: {
    alias: 'i',
    description: 'install template', // 命令对应的描述
    examples: [ // 命令对应的模板
      'zf-cli i',
      'zf-cli install',
    ],
  },
  config: {
    alias: 'c',
    description: 'config .zfclirc',
    examples: [
      'zf-cli config set <k> <v>',
      'zf-cli config get <k>',
      'zf-cli config remove <k>',
    ],
  },
  '*': {
    alias: '',
    description: 'not found',
    examples: [],
  },
};

Object.keys(actionMap).forEach((action) => {
  program.command(action)
    .description(actionMap[action].description)
    .alias(actionMap[action].alias)
    .action(() => {
      // 判断一下当前用的是什么操作
      if (action === '*') {
        console.log(actionMap[action].description);
      } else {
        // 实现更改配置文件
        console.log(process.argv);
        apply(action, process.argv.slice(3));
      }
    });
});

function help_b() {
  console.log('\r\n  ' + 'how to use command');
  Object.keys(actionMap).forEach((action) => {
    actionMap[action].examples.forEach((example) => {
      console.log('  - ' + example);
    });
  });
}

program.on('-h', help_b);
program.on('--help', help_b);
program.version(VERSION, '-v --version').parse(process.argv);
